<?php
namespace Home\Model;
use  Think\Model;
class IndexModel extends Model{

    protected $autoCheckFields =false;
    public function count_goods($where='1 = 1')
    {
        $DB_PREFIX=C('DB_PREFIX');
      
        // 取出所有符合条件的商品数据，并将结果存入对应的推荐类型数组中
        $sql = 'SELECT count(*) as num ' 
            . 'FROM ' . $DB_PREFIX . 'goods ' ;
        $sql .= ' WHERE is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 AND ' . $where;

        $result = M()->query($sql);


        return $result[0]['num'];
    }

 /**
     * 获取推荐商品
     * @param  $type
     * @param  $limit
     * @param  $start
     */
    public function goods_list($session=array(),$type = 'best', $limit = 10, $start = 0) {

        $no_pic='data/common/images/no_picture.gif';//没有产品时

    	$DB_PREFIX=C('DB_PREFIX');

        if ($type == 'new') {
            $type = 'g.is_new = 1';
        } else if ($type == 'hot') {
            $type = 'g.is_hot = 1';
        } else {
            $type = '1 = 1';
        }       


        // 取出所有符合条件的商品数据，并将结果存入对应的推荐类型数组中
        $sql = 'SELECT g.goods_id,g.sales_volume_base, g.goods_name, g.click_count, g.goods_name_style, g.market_price,'
            .' g.shop_price AS org_price, g.promote_price, ' . "IFNULL(mp.user_price, g.shop_price * "
            ."'$session[discount]') AS shop_price, " . "promote_start_date, promote_end_date,"
            ." g.goods_brief, g.goods_thumb, g.goods_img, RAND() AS rnd,xl.sales_volume " 
            . 'FROM ' . $DB_PREFIX . 'goods AS g ' 
            . "LEFT JOIN " . $DB_PREFIX . "member_price AS mp " 
            . "ON mp.goods_id = g.goods_id AND mp.user_rank = '$session[user_rank]' "
            .' LEFT JOIN '. $DB_PREFIX .'touch_goods AS xl ON g.goods_id=xl.goods_id ';
        $sql .= ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND ' . $type;
        $sql .= ' ORDER BY g.sort_order, g.last_update DESC limit ' . $start . ', ' . $limit;




        $result = M()->query($sql);





        foreach ($result as $key => $vo) {
            

            if ($vo['promote_price'] > 0) {



                $promote_price = bargain_price($vo['promote_price'], $vo['promote_start_date'], $vo['promote_end_date']);

                $goods[$key]['promote_price']= $promote_price > 0 ? price_format($promote_price): '';
            } else {
                $goods[$key]['promote_price'] = '';
            }  
             //计算属性的最低价格之和
             // $msum_properties_price = D('Goods')->msum_properties_price($vo['goods_id']);


             // if($msum_properties_price)
             // {
             //     $vo['market_price']=$vo['market_price']+floatval($msum_properties_price);
             //     $vo['shop_price']=$vo['shop_price']+floatval($msum_properties_price);
             //     if ($vo['promote_price'] > 0)
             //     $goods[$key]['promote_price']=price_format($promote_price+floatval($msum_properties_price));

             // }

             

            $goods[$key]['id'] = $vo['goods_id'];
            // $goods[$key]['goods_id'] = $vo['goods_id'];
            $goods[$key]['name'] = $vo['goods_name'];
            // $goods[$key]['brief'] = $vo['goods_brief'];

            // $goods[$key]['short_name'] = sub_str($vo['goods_name'], 7);
            $goods[$key]['market_price'] = price_format($vo['market_price']);
            $goods[$key]['shop_price'] = price_format($vo['shop_price']);
            $goods[$key]['goods_thumb'] = $vo['goods_thumb']?(get_path_url().$vo['goods_thumb']):(get_path_url().$no_pic);
            $goods[$key]['goods_img'] = $vo['goods_img']?(get_path_url().$vo['goods_img']):(get_path_url().$no_pic);
            $goods[$key]['url'] = 'http://'.$_SERVER['HTTP_HOST'].U('goods/index', array('id' => $vo['goods_id']));


            $vo['sales_volume'] = $vo['sales_volume']?$vo['sales_volume']:0;//销量
             if(empty($vo['sales_volume']))//销量统计增加
             {
                // $nCount=0;
                $nCount =M("touch_goods")->where('goods_id = "' . $vo['goods_id'] . '"')->count();

                if($nCount<1)
                {
                   M()->execute("INSERT INTO " . $DB_PREFIX . "touch_goods (`goods_id` ,`sales_volume` ) VALUES ( '" . $vo['goods_id'] . "' , '0')");
                }
                 
             }
              $goods[$key]['sales_volume'] = $vo['sales_volume_base']+$vo['sales_volume'];


            // $goods[$key]['click_count'] = $vo['click_count'];//点击量
            // $goods[$key]['sc'] = D('GoodsBase')->get_goods_collect($vo['goods_id']);//收藏人数
            // $goods[$key]['mysc'] = 0;//用户是否收藏
            // 检查是否已经存在于用户的收藏夹 collect_goods

            //所有的促销活动信息
            // $goods[$key]['promotion'] = model('GoodsBase')->get_promotion_show($vo['goods_id']);


             // if ($type == 'zk') {
             //    if($goods[$key]['promotion']==null)
             //        {
             //          unset($goods[$key]);
             //        }
             // }
             // else
                  $type_goods[$type][] = $goods[$key];
          
        }
        
    /*  exit;*/
   /*  var_dump($type_goods[$type]);*/
        return $type_goods[$type];
    }



}
?>
